package com.jic.market.impl.coupon;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.common.utils.HttpClientUtils;
import com.jic.market.entity.coupon.CouponDetail;
import com.jic.market.mapper.coupon.CouponDetailMapper;
import com.jic.market.service.coupon.CouponDetailService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 优惠券明细表
 *
 * @author : seven
 * @version : v1.0.0
 * @email : 774329481@qq.com
 * @since : 2020-03-16 05:05:22
 */
@Service
public class CouponDetailServiceImpl implements CouponDetailService {

    @Autowired
    private CouponDetailMapper couponDetailMapper;

    @Override
    public long insert(CouponDetail couponDetail) {
        long flag = couponDetailMapper.insert(couponDetail);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return couponDetailMapper.delete(id);
    }

    @Override
    public long update(CouponDetail couponDetail) {
        return couponDetailMapper.update(couponDetail);
    }

    @Override
    public CouponDetail queryByPrimaryKey(Long id) {
        return couponDetailMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<CouponDetail> query(CouponDetail couponDetail) {
        return couponDetailMapper.query(couponDetail);
    }

    @Override
    public PageResult<CouponDetail> queryPage(CouponDetail couponDetail,
                                              Page pageQuery,
                                              Date createStartTime,
                                              Date createEndTime,
                                              Date useStartTime,
                                              Date useEndTime) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<CouponDetail> list = couponDetailMapper.queryPage(startIndex,
                pageQuery.getLimit(),
                couponDetail,
                createStartTime,createEndTime,useStartTime,useEndTime);
        long count = couponDetailMapper.queryPageCount(couponDetail,createStartTime,
                createEndTime,
                useStartTime,
                useEndTime);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public RestResult<String> updateCouponStatus(String couponCode) {
        couponDetailMapper.updateStatus(new ArrayList<String>(){{
            add(couponCode);
        }},2);
        return RestResult.success("成功");
    }
}